﻿using FineUI;
using SysManage.BLL;
using System;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;

namespace SysManage.Module.TreasureBox
{
    public partial class TreasureBoxEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        public string Category
        {
            get
            {
                return this.Request["category"] ?? "";
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            if (this.Category == "Classical")
            {
                ddlSongType.Hidden = false;
                fileupload.Label = "上传歌曲";
            }
            else
            {
                fileupload.Label = "上传文档";
            }

            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyArticle article = DB.PartyArticles.Find(id);
                if (article == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtTitle.Text = article.Title;
                txtContent.Text = article.Content;

                if (this.Category == "Classical")
                {
                    ddlSongType.SelectedValue = article.ArticlesLable;
                }
            }
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;

            if (id != "")
            {
                //更新
                PartyArticle article = DB.PartyArticles.Find(id);
                if (article == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                article.Title = txtTitle.Text.Trim();
                article.Content = txtContent.Text.Trim();
                article.ModifyTime = DateTime.Now;
                article.ModifyBy = CurrentUser.LoginID;

                if (!string.IsNullOrEmpty(hdFileID.Text))
                {
                    article.ImageID = hdFileID.Text;
                }
                if (this.Category == "Classical")
                {
                    article.ArticlesLable = ddlSongType.SelectedValue;
                }

                DB.SaveChanges();
            }
            else
            {
                PartyArticle article = new PartyArticle();
                article.ID = StringHelper.NewID;
                article.Title = txtTitle.Text.Trim();
                
                //若Category等于Download时，说明是干部工作-下载中心模块
                if (this.Category== "Download")
                    article.Module = "CadreWork";
                else
                    article.Module = "TreasureBox";

                article.Category = this.Category;
                article.Status = 4;
                article.Content = txtContent.Text.Trim();
                article.CreateTime = DateTime.Now;
                article.ModifyTime = DateTime.Now;
                article.CreateBy = CurrentUser.LoginID;
                article.ModifyBy = CurrentUser.LoginID;
                if (!string.IsNullOrEmpty(hdFileID.Text))
                {
                    article.ImageID = hdFileID.Text;
                }
                if (this.Category == "Classical")
                {
                    article.ArticlesLable = ddlSongType.SelectedValue;
                }

                DB.PartyArticles.Add(article);

                DB.SaveChanges();
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void fileupload_FileSelected(object sender, EventArgs e)
        {
            var rs = UpFileBusiness.Upload(fileupload, CurrentUser.LoginID);

            if (rs.IsSuccess)
            {
                hdFileID.Text = rs.FileID;
            }
        }
        #endregion
    }
}